<div class="dis-Table" id="body-div">
	<div class="border-right" style="width:200px;background-color: #f5f5f5;">
		<div class="treeShowScroll">
			<div id="getAll" class="all-tree" style="cursor:pointer;">
				<i class="fa fa-list-ul" aria-hidden="true" ></i> <span></span>
			</div>
			<div class="search-jstree" >
				<input id="search-jstree-input" style="height: 25px;"/>
				<img class="middle fa fa-search"  src="static/img/search-input_wev8.png">
			</div>
			<div id="carKindjsTree" style="background-color: rgb(245,245,245)!important;"></div>
		</div>
	</div>	
	<div class="layui-fluid">
		<div class="layui-card">
			<h5 class="rightTbTitle"><a id="titleBar" style="color:#696a6c;">模型管理</a></h5>
			<div class="layui-card-body">
				<form id="searchForm" class="searchForm form-inline">
					<input id="treeId" name="treeId" type="hidden" value="0" />
					<input id="treeName" type="hidden" value="" />
					<div class="searchDiv positionSpc">
						<div class="form-group pull-left important">
							<span>流程分类</span>
							<select id="categoryList" name="category" component-type="select" class="form-control">
								<option value=""></option>
							</select>
						</div>
					</div>
				</form>
				
				<div id="toolbar" class="toolbar">
					<!-- <button id="addBtn" class="btn btn-sm btn-success">
						<i class="glyphicon glyphicon-plus"></i> 添加
					</button>  -->
					<!--<button id="editBtn" class="btn btn-sm btn-primary">
						<i class="glyphicon glyphicon-edit"></i> 编辑
					</button>
					<button id="removeBtn" class="btn btn-sm btn-danger">
						<i class="glyphicon glyphicon-remove"></i> 删除
					</button>-->
					<div class="pull-right">
						<a id="searchBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-search"></i> <font>查询</font></a>
						<a id="resetBtn" class="btn btn-success btn-bordered btn-sm"><i class="fa fa-refresh"></i> <font>重置</font></a>
						<a id="easySearch" class="btn btn-success btn-bordered btn-sm" style="display: none;"><i class="fa fa-backward"></i> <font>返回</font></a>
						<a id="hardSearch" class="btn btn-success btn-bordered btn-sm" style="display: inline-block;"><i class="fa fa-search-plus"></i> <font>高级查询</font></a>
					</div>
				</div>

				<table id="table-list"></table>

			</div>
		</div>
	</div>
</div>

<script>
    layui.use(['common', 'view', 'admin', 'laytpl'], function () {
        var $ = layui.$
            , R = layui.common
            , view = layui.view
            , admin = layui.admin
            , laytpl = layui.laytpl;
		
        //这段必须写，防止id重复
        var getTime = "-" + new Date().getTime();
        $("#body-div").attr("id", "body-div" + getTime);
        $("#table-list").attr("id","table-list" + getTime);

        var btnsStr = LayerUtil.getBtnsStrFromMenuData();
		
		var bootstrapTableUtil = new BootstrapTableUtil(getTime,true);
		bootstrapTableUtil.init({
			//url: 'http://127.0.0.1:5500/json/getModelList.json',
			url: admin.basePath + '/service/model/list',
			columns: [
                {
					checkbox: true
				} 
                , { field: 'category', title: '流程分类', sortable: true }
                , { field: 'name', title: '模型名称', sortable: true }
                , { field: 'officeName', title: '归属机构', sortable: true }
				, { field: 'metaInfo.scope', title: '表单路径', sortable: true, align: 'center'}
                , {
                    field: "lastUpdateTime", 
                    title: "最后更新时间"
                    // formatter: function (value, row, index) { 
                    // 	return row.lastUpdateTime
                    // }
                }
                , { 
                	title: layui.admin.lang() ? 'Operation' : '操作',
                	width: '45px',
					"class": 'center must',
					events: {
						'click .disignModel': function (e, value, row, index) {
							//在线设计
							console.log(row.id);
							var href = admin.host + '/modeler.html?modelId='+row.id+'&ctx=/gtoa/a';
							window.open(href);
						},
						'click .setModel': function (e, value, row, index) {
							//编辑
							//console.log(row.id);
							//var id = row.id;
							LayerUtil.openDialog({
								title: layui.admin.lang() ? 'Form' : '表单',
								formLay: 'wytest-form',
								initData:{
									modelId : row.id
								},
								url: 'admin/processCenter/form',
								//actionUrl: 'http://127.0.0.1:5500/json' + '/GetForm.json',
								actionUrl: admin.basePath + '/service/model/data?modelId='+row.id,
					            btn: ['保存', '关闭'],
					            btn_EN: ['save', 'close'],
					            func1:function(index,layero){
				                	window.processCenter.dosubmit(index,row.id);
					            	bootstrapTableUtil.refresh();
					            }
							})
						},
						'click .deployModel': function (e, value, row, index) {
							//部署
							LayerUtil.confirm('确认要部署该模型吗？', function() {
								LayerUtil.ajax({
									url: admin.basePath + '/service/model/deploy',
									type:'get',
									shade:true,
									data: {id:row.id},
									success: function(res){
										LayerUtil.success(res[LayerUtil.getMsgLang()]);
										bootstrapTableUtil.refresh();
									}
								});
							});
						},
						'click .deleteModel': function (e, value, row, index) {
							//部署
							LayerUtil.confirm('确认要删除该模型吗？', function() {
								LayerUtil.ajax({
									url: admin.basePath + '/service/model/delete',
									type:'get',
									shade:true,
									data: {id:row.id},
									success: function(res){
										LayerUtil.success(res[LayerUtil.getMsgLang()]);
										bootstrapTableUtil.refresh();
									}
								});
							});
						}
					},
					formatter: function(value, row, index) {
						return btnsStr.tableBtnsStr;
					}
                }
            ]
		},"wytest-list");
		
		LayerUtil.ajax({
			type:'get',
			url: admin.basePath + "/sys/dict/dictList?type=261f40db1acd423f89e3f86a736ef3fa",
    		shade:true,
			success: function(result){
				var html  = "";
				$.each(result.data.dict, function(i,obj){
					html += "<option value='"+obj.member+"'>"+obj.memberName+"</option>"
				});
				$("#categoryList").append(html);
				myHelper.initSelect2($("#categoryList"));
			}
		})
		
		$("#body-div"+getTime).find("#addModel").on("click",function(){
			addOrEdit();
		});
		
    	// $("#body-div"+getTime).find("#editBtn").on("click",function(){
		// 	var ids = bootstrapTableUtil.getSingleId();
		// 	if(ids){
		// 		addOrEdit({
		// 			id:ids
		// 		});
		// 	}
		// });
		$("#body-div"+getTime).find("#removeBtn").on("click",function(){
			var ids = bootstrapTableUtil.getMultiId();
			if(ids){
				LayerUtil.confirm(layui.admin.lang() ? "are you sure?" : "确定删除吗？", function(){
					LayerUtil.ajax({
		        		url: admin.basePath + '/true.json',
		        		data: {
		        			ids: ids
		        		},
		        		success: function(res){
		        			LayerUtil.success(res[LayerUtil.getMsgLang()]);
		        			bootstrapTableUtil.refresh();
		        		}
		        	})
				})
	       	}
		});
		function addOrEdit(initData){
			LayerUtil.openDialog({
				title: layui.admin.lang() ? 'Form' : '添加模型',
				formLay: 'sys_dictionary',
				initData:{
					office:{
						id:$("#body-div"+getTime).find('#treeId').val() == 0 ? "" : $("#body-div"+getTime).find('#treeId').val(),
						name:$("#body-div"+getTime).find('#treeName').val()
					}
				},
				url: 'admin/processCenter/formAdd',
				setDataWithoutAjax:true,
	            btn: ['保存', '关闭'],
	            btn_EN: ['save', 'close'],
	            func1:function(index,layero){
					var dialogDom = $(layero).find("#inputForm");
					var data = {
						name: $(dialogDom).find("input[name='name']").val(),
						key: $("#body-div"+getTime).find('#treeId').val(),
						description: $(dialogDom).find('textarea[name="metaInfo.description"]').val(),
						category: $(dialogDom).find('select[name="category"]').val(),
						scope: $(dialogDom).find("input[name='metaInfo.scope']").val(),
						officeId: $(dialogDom).find("input[name='office.id']").val()
					}
					//name，key，description，category，scope，officeId
	            	LayerUtil.ajax({
                		url: admin.basePath + '/service/model/create',
                		data: data,
                		success: function(res){
                			LayerUtil.success(res[LayerUtil.getMsgLang()]);
                			bootstrapTableUtil.refresh();
                			layui.layer.close(index)
                		}
                	});
	            }
			})
		}
		
		//渲染左树
		function refresh(node){
			$("#body-div"+getTime).find('#treeId').val(node.id);
			$("#body-div"+getTime).find('#treeName').val(node.text);
			bootstrapTableUtil.refresh();
			// 改变栏目
			var html = layui.admin.lang() ? 'Model management' : '模型管理 ';
			if(node.text){
				node.text = " ："+node.text;
			}
			$("#body-div"+getTime).find("#titleBar").html(html+node.text);
		}
		
		/*var jsTreeUtil = new JsTreeUtil(getTime,{
			url:admin.basePath + '/system/office/viewData',
			type:'get',
			actionDataName: "list"
		});*/
		var jsTreeUtil = new JsTreeUtil(getTime,{
			url:admin.basePath + '/system/office/viewData',
			type:'get',
			actionDataName: "list"
		});
		jsTreeUtil.init(refresh, layui.admin.lang() ? "All types": "全部类型");
   });
</script>